﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyRapChieuPhim.BUS;
using QuanLyRapChieuPhim.DTO;

namespace QuanLyRapChieuPhim.GUI
{
    public partial class uscThemSuatChieu : UserControl
    {
        int Ma = -1;
        bool IsLoaded = false;

        public uscThemSuatChieu()
        {
            InitializeComponent();
        }

        private void uscThemSuatChieu_Load(object sender, EventArgs e)
        {
            Ma = int.Parse(Tag.ToString());
            LoadDefault();
            IsLoaded = true;
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            Ma = 0;
            if (!Check())
                return;
            SuatChieuBUS BUS = new SuatChieuBUS();
            SuatChieuDTO DTO = new SuatChieuDTO
                (
                    0, txtTenSuatChieu.Text, int.Parse(cbCaChieu.SelectedValue.ToString()),
                   int.Parse(cbPhongChieu.SelectedValue.ToString()), int.Parse(cbPhim.SelectedValue.ToString()),
                   int.Parse(txtSoLuongVe.Text), DateTime.Parse(dtpNgayChieu.Text)
               );
            if (BUS.Add(DTO))
            {
                MessageBox.Show("Thêm thành công.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.None);
                return;
            }
            MessageBox.Show("Thêm thất bại.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.None);
        }

        private void btnCapNhap_Click(object sender, EventArgs e)
        {
            Ma = int.Parse(Tag.ToString());
            if (!Check())
                return;
            SuatChieuBUS BUS = new SuatChieuBUS();
            SuatChieuDTO DTO = new SuatChieuDTO
                (
                    Ma, txtTenSuatChieu.Text, int.Parse(cbCaChieu.SelectedValue.ToString()),
                   int.Parse(cbPhongChieu.SelectedValue.ToString()), int.Parse(cbPhim.SelectedValue.ToString()),
                   int.Parse(txtSoLuongVe.Text), DateTime.Parse(dtpNgayChieu.Text)
               );
            if (BUS.Update(DTO))
            {
                MessageBox.Show("Cập nhật thành công.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.None);
                return;
            }
            MessageBox.Show("Cập nhật thất bại.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.None);

        }

        private void btnDienLai_Click(object sender, EventArgs e)
        {
            IsLoaded = false;
            LoadDefault();
            IsLoaded = true;
        }

        private void cbRap_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (IsLoaded)
            {
                cbPhongChieu.DataSource = new PhongChieuBUS().GetList_Rap(int.Parse(cbRap.SelectedValue.ToString()));
                cbPhongChieu.DisplayMember = "TenPhongChieu";
                cbPhongChieu.ValueMember = "MaPhongChieu";
                cbPhongChieu.SelectedIndex = 0;
                dtpNgayChieu.Text = DateTime.Now.ToShortDateString();
            }
        }

        private void cbPhongChieu_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (IsLoaded)
            {
                int sove = ((PhongChieuDTO)cbPhongChieu.SelectedItem).SoHangGhe * ((PhongChieuDTO)cbPhongChieu.SelectedItem).SoDayGhe;
                txtSoLuongVe.Text = sove.ToString();
                dtpNgayChieu.Text = DateTime.Now.AddDays(1).ToShortDateString();
            }
        }

        void LoadDefault()
        {
            try
            {
                lbsao01.Visible = false;
                lbsao01.Visible = false;

                cbCaChieu.DataSource = new CaChieuBUS().GetList();
                cbCaChieu.DisplayMember = "TenCaChieu";
                cbCaChieu.ValueMember = "MaCaChieu";

                cbRap.DataSource = new RapBUS().GetList();
                cbRap.DisplayMember = "TenRap";
                cbRap.ValueMember = "MaRap";

                cbPhim.DataSource = new PhimBUS().GetList();
                cbPhim.DisplayMember = "TenPhim";
                cbPhim.ValueMember = "MaPhim";

                cbPhongChieu.DataSource = new PhongChieuBUS().GetList();
                cbPhongChieu.DisplayMember = "TenPhongChieu";
                cbPhongChieu.ValueMember = "MaPhongChieu";

                SuatChieuBUS BUS = new SuatChieuBUS();
                SuatChieuDTO DTO = new SuatChieuDTO();
                DTO = BUS.GetFromId(Ma);

                if (DTO.NgayChieu.ToString() == "01/01/0001 12:00:00 AM")
                {
                    dtpNgayChieu.Text = DateTime.Now.ToShortDateString();
                    cbRap.SelectedIndex = 0;

                    txtSoLuongVe.Text = "0";
                    txtTenSuatChieu.Text = "";
                    cbCaChieu.SelectedIndex = 0;
                    cbPhim.SelectedIndex = 0;
                    cbPhongChieu.SelectedIndex = 0;
                }

                else
                {
                    dtpNgayChieu.Text = DTO.NgayChieu.ToString();

                    cbRap.SelectedValue = new PhongChieuBUS().IdRapFromID(DTO.MaPhongChieu);

                    txtSoLuongVe.Text = DTO.SoLuongVe.ToString();
                    txtTenSuatChieu.Text = DTO.TenSuatChieu;
                    cbCaChieu.SelectedValue = DTO.MaCaChieu;
                    cbPhim.SelectedValue = DTO.MaPhim;
                    cbPhongChieu.SelectedValue = DTO.MaPhongChieu;
                }
            }
            catch (Exception)
            {

            }

        }

        bool Check()
        {
            lbsao01.Visible = false;
            lbThongBao01.Visible = false;
            lbTBNgayChieu.Visible = false;

            if (txtTenSuatChieu.Text == "")
            {
                lbsao01.Visible = true;
                txtTenSuatChieu.Focus();
                return false;
            }
            else if (DateTime.Parse(dtpNgayChieu.Text) < DateTime.Now)
            {
                lbTBNgayChieu.Visible = true;
                dtpNgayChieu.Focus();
                return false;
            }
            else if (new SuatChieuBUS().CheckExists(Ma, txtTenSuatChieu.Text))
            {
                lbThongBao01.Visible = true;
                txtTenSuatChieu.Focus();
                return false;
            }
            return true;
        }
    }
}
